package com.njmaomao.gj.pyot.common.bean;

import java.io.Serializable;

import com.njmaomao.gj.pyot.common.ResponseCode;
import com.njmaomao.gj.pyot.common.Utils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(value = "操作结果")
public class Response<T> extends BaseResponse implements Serializable {

    private static final long serialVersionUID = 254217558609168068L;

    @ApiModelProperty(notes = "业务数据")
    private T data;

    public static <T> Response<T> success() {
        return success((String) null, null);
    }
    
    public static <T> Response<T> success(String seqNo) {
        return success(seqNo, null);
    }

    public static <T> Response<T> success(BaseRequest request) {
        return success(request, null);
    }


    public static <T> Response<T> success(BaseRequest request, T data) {
        return success(request == null ? "" : request.getSeqNo(), data);
    }

    public static <T> Response<T> success(String seqNo, T data) {
        Response<T> response = new Response<>();
        response.setSeqNo(seqNo);
        response.setCode(ResponseCode.COMMON.SUCCESS.getCode());
        response.setDesc(ResponseCode.COMMON.SUCCESS.getDesc());
        response.setData(data);
        return response;
    }

    public static <T> Response<T> fail(String seqNo) {
        return fail(seqNo, null, null, null);
    }

    public static <T> Response<T> fail(String seqNo, String code, String desc) {
        return fail(seqNo, code, desc, null);
    }

    public static <T> Response<T> fail(String seqNo, String code, String desc, T data) {
        return getInstance(seqNo, code, desc, data);
    }

    public static <T> Response<T> fail(String seqNo, ResponseCode responseCode) {
        return getInstance(seqNo, responseCode.getCode(), responseCode.getDesc(), null);
    }

    public static <T> Response<T> fail(String seqNo, ResponseCode responseCode, T data) {
        return getInstance(seqNo, responseCode.getCode(), responseCode.getDesc(), data);
    }

    public static <T> Response<T> copy(BaseResponse response) {
        return copy(response, null);
    }

    public static <T> Response<T> copy(BaseResponse response, T data) {
        String seqNo = response != null ? response.getSeqNo() : null;
		String code = response != null ? response.getCode() : ResponseCode.COMMON.API_ERROR.getCode();
		String desc = response != null ? response.getDesc() : ResponseCode.COMMON.API_ERROR.getDesc();
		return getInstance(seqNo, code, desc, data);
    }

    public static <T> Response<T> getInstance(String seqNo) {
        return getInstance(seqNo, null, null, null);
    }

    public static <T> Response<T> getInstance(String seqNo, String code, String desc) {
        return getInstance(seqNo, code, desc, null);
    }

    public static <T> Response<T> getInstance(String seqNo, String code, String desc, T data) {
        Response<T> response = new Response<>();
        response.setSeqNo(seqNo);
        response.setCode(code);
        response.setDesc(desc);
        response.setData(data);
        return response;
    }
    
    public static boolean isSuccess(BaseResponse baseResponse) {
    	return baseResponse != null && ResponseCode.COMMON.SUCCESS.getCode().equals(baseResponse.getCode());
    }
    
    public static boolean isFailure(BaseResponse baseResponse) {
    	return !isSuccess(baseResponse);
    }
    
    public boolean isSuccess() {
    	return ResponseCode.COMMON.SUCCESS.getCode().equals(getCode());
    }

    @Override
    public String toString() {
        return Utils.toJson(this);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
    
}